package com.liuguang.framework.utils;

import android.content.ClipboardManager;
import android.content.Context;
import android.widget.Toast;
import com.bumptech.glide.load.Key;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class StringUtils {
    public static String filterNull(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0 || "null".equalsIgnoreCase(str);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isBlank(String str) {
        if (isEmpty(str)) {
            return true;
        }
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static String toString(String str) {
        return isBlank(str) ? "" : str;
    }

    public static String toStringWithoutBlank(String str) {
        return isEmpty(str) ? "" : str;
    }

    public static boolean isEmpty(String[] strArr) {
        return strArr == null || strArr.length <= 0;
    }

    public static boolean isNotEmpty(String[] strArr) {
        return strArr != null && strArr.length > 0;
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return collection != null && !collection.isEmpty();
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    public static boolean isNotEmpty(Map<?, ?> map) {
        return map != null && !map.isEmpty();
    }

    public static String getLimitLengthString(String str, int i) {
        if (i < 0) {
            return str;
        }
        try {
            return getLimitLengthString(str, null, i, null);
        } catch (UnsupportedEncodingException unused) {
            return str;
        }
    }

    public static String getLimitLengthString(String str, int i, String str2) {
        if (i < 0) {
            return str;
        }
        try {
            return getLimitLengthString(str, null, i, str2);
        } catch (UnsupportedEncodingException unused) {
            return str;
        }
    }

    public static String getLimitLengthString(String str, String str2, int i, String str3) throws UnsupportedEncodingException {
        byte[] bArr;
        if (str == null || str.length() == 0) {
            return str;
        }
        if (str3 == null) {
            str3 = "";
        }
        if (str2 == null) {
            bArr = str.getBytes(Key.STRING_CHARSET_NAME);
        } else {
            bArr = str.getBytes(str2);
        }
        if (bArr.length <= i) {
            return str;
        }
        if (i > str3.length()) {
            i -= str3.length();
        }
        int i2 = 0;
        for (int i3 = 0; i3 < i; i3++) {
            if (bArr[i3] < 0) {
                i2++;
            }
        }
        if (i2 % 2 != 0) {
            i--;
        }
        byte[] bArr2 = new byte[i];
        System.arraycopy(bArr, 0, bArr2, 0, i);
        if (str2 == null) {
            StringBuilder sb = new StringBuilder();
            sb.append(new String(bArr2, Key.STRING_CHARSET_NAME));
            sb.append(str3);
            return sb.toString();
        }
        StringBuilder sb2 = new StringBuilder();
        sb2.append(new String(bArr2, str2));
        sb2.append(str3);
        return sb2.toString();
    }

    public static String toHTML(String str) {
        return toHTML(str, true);
    }

    public static String toHTML(String str, boolean z) {
        if (isEmpty(str)) {
            return "";
        }
        char[] cArr = new char[str.length()];
        str.getChars(0, str.length(), cArr, 0);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < cArr.length; i++) {
            char c = cArr[i];
            if (c == 10) {
                stringBuffer.append("<br/>");
            } else if (c != 13) {
                if (c != ' ') {
                    if (c == '\"') {
                        stringBuffer.append("&quot;");
                    } else if (c == '<') {
                        stringBuffer.append("&lt;");
                    } else if (c != '>') {
                        switch (c) {
                            case '&':
                                stringBuffer.append("&amp;");
                                break;
                            case '\'':
                                stringBuffer.append("&#039;");
                                break;
                            default:
                                stringBuffer.append(cArr[i]);
                                break;
                        }
                    } else {
                        stringBuffer.append("&gt;");
                    }
                } else if (z) {
                    stringBuffer.append("&nbsp;");
                } else {
                    stringBuffer.append(cArr[i]);
                }
            }
        }
        return stringBuffer.toString();
    }

    public static String oppHTML(String str) {
        return isEmpty(str) ? "" : str.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("<br/>", "\n").replaceAll("&amp;", "&").replaceAll("&#039;", "'").replaceAll("&quot;", "\"").replaceAll("&nbsp;", " ");
    }

    public static String subString(String str, int i, int i2) {
        if (isEmpty(str)) {
            return "";
        }
        if (i < 0) {
            i = 0;
        } else if (i >= str.length()) {
            i = str.length() - 1;
        }
        if (i2 < 0) {
            i2 = 0;
        } else if (i2 >= str.length()) {
            i2 = str.length() - 1;
        }
        return str.substring(i, i2);
    }

    public static int checkLength(String str) {
        int i = 0;
        if (isEmpty(str) || isEmpty(str.trim())) {
            return 0;
        }
        String trim = str.trim();
        while (Pattern.compile("[\\u4e00-\\u9fa5]").matcher(trim).find()) {
            i++;
        }
        return trim.length() + i;
    }

    public static String SpecialWaste(String str) {
        if (str == null) {
            return null;
        }
        return SpecialWaste(str, 0, str.length() - 1);
    }

    public static String SpecialWaste(String str, int i, int i2) {
        if (str == null || i < 0 || i >= i2 || i2 >= str.length()) {
            return str;
        }
        try {
            String substring = str.substring(0, i);
            String substring2 = str.substring(i2, str.length() - 1);
            String substring3 = str.substring(i, i2);
            Pattern compile = Pattern.compile("\\s*|\t|\r|\n");
            StringBuilder sb = new StringBuilder();
            sb.append(substring);
            sb.append(compile.matcher(substring3).replaceAll(""));
            sb.append(substring2);
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        }
    }

    public static String stringFilter(String str) throws PatternSyntaxException {
        return str.replaceAll("[\\\\^/:*?\"<>|]+", "").trim();
    }

    public static String makeupStringWithSplit(String str, String... strArr) {
        StringBuilder sb = new StringBuilder();
        if (strArr != null && strArr.length > 0) {
            sb.append(strArr[0]);
            for (int i = 1; i < strArr.length; i++) {
                if (str != null) {
                    sb.append(str);
                }
                sb.append(strArr[i]);
            }
        }
        return sb.toString();
    }

    public static String makeupString(String... strArr) {
        return makeupStringWithSplit(null, strArr);
    }

    public static String makeupStringWithUnderline(String... strArr) {
        return makeupStringWithSplit("_", strArr);
    }

    public static void copyText(Context context, String str) {
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService("clipboard");
        if (clipboardManager != null) {
            clipboardManager.setText(str);
            Toast.makeText(context, "复制成功！", 0).show();
        }
    }

    public static String sort(String str) {
        char[] charArray = str.toCharArray();
        Arrays.sort(charArray);
        return new String(charArray);
    }

    public static String getMaxSubString(String str, String str2) {
        String str3 = str.length() > str2.length() ? str : str2;
        if (str3 == str) {
            str = str2;
        }
        for (int i = 0; i < str.length(); i++) {
            int i2 = 0;
            for (int length = str.length() - i; length != str.length() + 1; length++) {
                String substring = str.substring(i2, length);
                if (str3.contains(substring)) {
                    return substring;
                }
                i2++;
            }
        }
        return "";
    }
}
